//
//  CDraft.m
//  HotDisPlus
//
//  Created by yilong zhang on 12-6-27.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CDraft.h"

@implementation CDraft

@synthesize m_lnID;
@synthesize m_lnTID;
@synthesize m_nSate;
@synthesize m_strName;
@synthesize m_strAddr;
@synthesize m_strResName;
@synthesize m_strSum;
@synthesize m_strCity;
@synthesize m_strProvince;
@synthesize m_strCountry;
@synthesize m_nX;
@synthesize m_nY;
@synthesize m_strMainImgUrl;
@synthesize m_strMainImgPath;
@synthesize m_strPath;
@synthesize m_nCoverIndex;

@synthesize m_Task;
@synthesize m_Image;
@synthesize m_bIsLoad;


+(int)CreateDraftTable:(NSString *)tablename
{
    NSMutableArray *keys = [NSMutableArray array];
    CKey *key = nil;
    
    key = [[CKey alloc] initWithType:1 Length:32 Name:@"D_AID" Primary:YES];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Length:32 Name:@"D_TID" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Length:4 Name:@"D_State" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:100 Name:@"D_Name" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:100 Name:@"D_ResName" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:150 Name:@"D_Addr" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:150 Name:@"D_Sum" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:150 Name:@"D_City" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:150 Name:@"D_Province" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:150 Name:@"D_Country" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:2 Length:32 Name:@"D_X" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:2 Length:32 Name:@"D_Y" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:150 Name:@"D_MainImgUrl" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:150 Name:@"D_MainImgPath" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:200 Name:@"D_Path" Primary:NO];
    [keys addObject:key];
    [key release];
    
    return [[CDBManager ScheduledShareInstance] CreateTable:tablename Keys:keys];
}

+(int)GetDraftTotal:(NSString*)tablename Where:(NSString*)where
{
    return [[CDBManager ScheduledShareInstance] GetRecordTotal:tablename Condition:where Key:@"D_AID"];
}

+(NSArray*)GetLimitDraft:(NSString*)tablename Where:(NSString*)where Offset:(int)offset Limit:(int)limit;
{
    NSArray         *keys  = [NSArray arrayWithObjects:@"*", nil];
    NSArray         *rets  = nil;
    NSMutableArray  *prdcts= [NSMutableArray array];
    CDraft        *prdct = nil;
    
    rets = [[CDBManager ScheduledShareInstance] QueryRecordsFromCursor:tablename Keys:keys Offset:offset Limit:limit Column:Draft_properties_Num Condition:where];
    
    if (rets && [rets count] > 0)
    {
        for (int i=0; i<[rets count]; i++)
        {
            prdct = [[CDraft alloc] init];
            [prdct SetDraftWithArr:[rets objectAtIndex:i]];
            [prdcts addObject:prdct];
            [prdct release];
        }
        
        return prdcts;
    }
    
    return nil;
}

+(int)UpdateDraftInfo:(NSString*)tablename Where:(NSString*)where Keys:(NSArray*)keys
{
    return [[CDBManager ScheduledShareInstance] UpdateRecordInfo:tablename Keys:keys Condition:where];
}

+(int)RemoveDraft:(NSString*)tablename Where:(NSString*)where
{
    return [[CDBManager ScheduledShareInstance] DeleteRecord:tablename Condition:where];
}

-(void)dealloc
{
    m_Image = nil;
    
    [m_lnID release];
    [m_lnTID release];
    [m_nSate release];
    [m_strName release];
    [m_strResName release];
    [m_strAddr release];
    [m_strSum release];
    [m_strCity release];
    [m_strProvince release];
    [m_strCountry release];
    [m_nX release];
    [m_nY release];
    [m_strMainImgUrl release];
    [m_strMainImgPath release];
    [m_strPath release];
    
    [self CancelDownLoadImage];
    
    [super dealloc];
}

-(void)SetDraftWithArr:(NSArray*)arr
{
    if (arr)
    {
        int nCnt = 0;
        id  objc = nil;
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_lnID  = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_lnTID  = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_nSate  = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_strName  = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_strResName = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_strAddr  = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_strSum  = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_strCity  = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_strProvince  = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_strCountry  = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_nX  = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_nY  = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_strMainImgUrl = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_strMainImgPath = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.m_strPath    = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
    }
}

-(void)SetDraftWithDic:(NSDictionary*)dic
{
    self.m_lnID  = [NSNumber numberWithLong:[[dic objectForKey:@"p_clientID"] intValue]];
    self.m_lnTID = [NSNumber numberWithLong:[[dic objectForKey:@"p_id"] intValue]];
    self.m_nSate = [NSNumber numberWithInt:1];
    self.m_strName = [dic objectForKey:@"p_name"];
    self.m_strResName = [dic objectForKey:@"p_restaurant"];
    self.m_strAddr = [dic objectForKey:@"p_addr"];
    self.m_strSum = [dic objectForKey:@"p_caption"];
    self.m_strCity = [dic objectForKey:@"p_city"];
    self.m_strProvince = [dic objectForKey:@"p_province"];
    self.m_strCountry = [dic objectForKey:@"p_country"];
    self.m_nX = [NSNumber numberWithFloat:[[dic objectForKey:@"p_x"] floatValue]];
    self.m_nY = [NSNumber numberWithFloat:[[dic objectForKey:@"p_y"] floatValue]];
    self.m_strMainImgUrl = [dic objectForKey:@"p_big"];
    self.m_strMainImgPath= [NSString stringWithFormat:@"%@/Documents/Draft/%@/%@.jpg", NSHomeDirectory(), [dic objectForKey:@"p_clientID"], [dic objectForKey:@"p_clientID"]];
    self.m_strPath = [NSString stringWithFormat:@"%@/Documents/Draft/%@", NSHomeDirectory(), [dic objectForKey:@"p_clientID"]];
}

-(void)CrreatePath
{
    [[AppConfig DefaultAppConfig] CreatePathInApp:@"Documents/Draft" SubPath:[NSString stringWithFormat:@"%ld", [self.m_lnID longValue]]];
}

-(int)SaveDraft:(NSString*)tablename
{
    NSMutableArray *keys = [NSMutableArray array];
    CKey *key = nil;
    
    key = [[CKey alloc] initWithType:1 Value:self.m_lnID Name:@"D_AID"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Value:self.m_lnTID Name:@"D_TID"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Value:self.m_nSate Name:@"D_State"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.m_strName Name:@"D_Name"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.m_strResName Name:@"D_ResName"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.m_strAddr Name:@"D_Addr"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.m_strSum Name:@"D_Sum"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.m_strCity Name:@"D_City"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.m_strProvince Name:@"D_Province"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.m_strProvince Name:@"D_Country"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:2 Value:self.m_nX Name:@"D_X"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:2 Value:self.m_nY Name:@"D_Y"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.m_strMainImgUrl Name:@"D_MainImgUrl"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.m_strMainImgPath Name:@"D_MainImgPath"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.m_strPath Name:@"D_Path"];
    [keys addObject:key];
    [key release];
    
    return [[CDBManager ScheduledShareInstance] InsertNewRecordToTable:tablename Keys:keys];
}

-(void)SaveMainImage:(NSString*)path Data:(NSData*)data
{
    if (data)
    {
        [data writeToFile:path atomically:YES];
    }
}

-(NSData*)GetMainImage
{
    //NSLog(@"%@", self.m_strMainImgPath);
    NSData *data = [NSData dataWithContentsOfMappedFile:self.m_strMainImgPath];
    if (!data)
    {
        [self DownLoadMainImage];
    }
    return data;
}

-(void)DownLoadMainImage
{
    if (!m_bIsLoad)
    {
        m_bIsLoad = YES;
        self.m_Task = [[CDownLoadManager DefaultDownLoadManager] AddTask:self.m_strMainImgUrl Sponsor:self PostData:nil TimeOut:30 Data:[NSNumber numberWithInt:0] MType:Get UserInfo:nil];
    }
}

-(void)CancelDownLoadImage
{
    m_Image = nil;
    
    [[CDownLoadManager DefaultDownLoadManager] CancelTask:m_Task];
    self.m_Task = nil;
}

-(void)DidOperationFinished:(COperation*)op Data:(NSData*)data Other:(id)other UserInfo:(id)userinfo
{
    int nCode = [other intValue];
    
    //mainimage
    if (0 == nCode)
    {
        if (data)
        {
            [self SaveMainImage:self.m_strMainImgPath Data:data];
            m_Image.image = [UIImage imageWithData:data];
            m_Image = nil;
        }
        m_bIsLoad = NO;
        self.m_Task = nil;
    }
}

-(void)DidOperationError:(COperation*)op Error:(NSError*)error Other:(id)other UserInfo:(id)userinfo
{
    int nCode = [other intValue];
    
    //mainimage
    if (0 == nCode)
    {
        m_Image = nil;
        m_bIsLoad = NO;
        self.m_Task = nil;
    }
}

-(void)DidOperationTimeOut:(COperation*)op Other:(id)other UserInfo:(id)userinfo
{
    int nCode = [other intValue];
    
    //mainimage
    if (0 == nCode)
    {
        m_Image = nil;
        m_bIsLoad = NO;
        self.m_Task = nil;
    }
}

@end
